<?php
namespace app\demo_record\model;

use think\Db;


/**
 * 配置表模型
 */
class ConfigTableModel
{
    public $table="dm_config";
    
    /**
     * 创建对象
     * @return self
     */
    static public function alloc($table="dm_config")
    {
        $new = new static;
        $new->table=$table;
        return $new;
    }

    public function load($where=[])
    {
        $fields=Db::table($this->table)->where($where)->select()->toArray();
        /** @var array $field */
        foreach($fields as $key => $field){
            if($field['type'] === "checkbox"){
                $field["value"] = $field["value"] ? 1 : 0 ;
            }
            elseif($field['type'] === "multiple"){
                $field["value"] = explode(",",$field["value"]);
            }
            elseif(in_array($field['type'],["location","files","images"])){
                $field["value"] = json_decode($field["value"],true);
            }
            if($field["params"]){
                $params=json_decode($field["params"],true);
                $field=array_merge($field, $params);
                unset($field["params"]);
            }
            $fields[$key]=$field;
        }
        return $fields;
    }
    public function create($fields,$group="")
    {
        $new_fields=[];
        foreach($fields as &$field){
            if($field['type'] === "checkbox"){
                $field["value"] = $field["value"] ? 1 : 0 ;
            }
            elseif($field['type'] === "multiple"){
                $field["value"] = implode(",",$field["value"]);
            }
            elseif(in_array($field['type'],["location","files","images"])){
                $field["value"] = json_encode($field["value"],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
            }

            $field["readable"] = $field["readable"] ? 1 : 0 ;
            $field["writable"] = $field["writable"] ? 1 : 0 ;
            $field["required"] = $field["required"] ? 1 : 0 ;

            $item=[];
            $params=[];
            foreach($field as $key => $value){
                if(in_array($key,["name","title","type","value","describe","readable","writable","required"])){
                    $item[$key] = $value;
                }
                else{
                    $params[$key] = $value;
                }
            }
            $item["params"]=json_encode($params,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
            $item["group"]=$group;
            $new_fields[]=$item;
        }
        $result=Db::table($this->table)->insertAll($new_fields);
        return $result;
    }
    public function update()
    {

    }
    public function updateValue($fields)
    {
        foreach($fields as &$field){
            if($field['type'] === "checkbox"){
                $field["value"] = $field["value"] ? 1 : 0 ;
            }
            elseif($field['type'] === "multiple"){
                $field["value"] = implode(",",$field["value"]);
            }
            elseif(in_array($field['type'],["location","files","images"])){
                $field["value"] = json_encode($field["value"],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
            }
            Db::table($this->table)->where(["id"=>$field["id"]])->update(["value"=>$field["value"]]);
        }
        return true;
    }
    public function clear()
    {
        
    }
}
